" ~/.vimrc
"
" Weiwei Jia <harryxiyou@gmail.com> (VIM) 2009, 9
"
" CHANGELOGS:
" Feb. 7, Merge brother Zhiqiang's into mine.
"
" Plugins:
" 1, vim-pathogen: super easy to install plugins
" 2, AutoTag: Update entries in a tags file automatically
" 3, Taglist: List all the tags in open file
" 4, Nerdtree: Display the directory path 
" 5, CtrlP: get file by regex

" This option stops vim from behaving in a strongly vi -compatible way.
set nocompatible

" Enable filetype detection
filetype on
" Enable loading plugin
filetype plugin on

syntax on

" pathogen plugin, just keep all plugin in the bundle dir.
execute pathogen#infect()
syntax on
filetype plugin indent on

" file autocomplete
set wildmode=longest,list,full
set wildmenu

" settings here
set fileencodings=utf-8,gb2312,us-ascii,cp936,gbk 
set encoding=utf-8
set number
set filetype=on
set history=5120
set background=dark
set autoindent
set smartindent
set showmatch
set ruler
set nohls
set cindent
set tabstop=4
set shiftwidth=4
set cursorline

" set linebreak
" set textwidth=72
set wrap

" do incremental searching
set incsearch
" high light the search content
set hlsearch

" backup
" keep a backup file
set backup
" create the vimbackup dir if it does not exist
let t:check_vimbackup=system("bash -c \"if [ ! -d ~/.vimbackup ]; then mkdir ~/.vimbackup; fi\"")
" keep all backup files in one central dir
set backupdir=~/.vimbackup
set backupcopy=yes

" map key shotcuts
" F2 in insert mode for paste toggle
set pastetoggle=<F2>
" open taglist
nmap <F8> :Tlist<CR>
" open NerdTree
map <F7> :NERDTreeToggle<CR>
" close nerdtree
nmap QQ :NERDTreeClose<CR>
nmap ZZ :wq!<CR>
" number toggle
map <F10> :let &number=1-&number <CR>
" map ctrlp
nmap <F6> :CtrlP<CR>

"Set the white bar
set laststatus=2
"set statusline=%<%([%{Tlist_Get_Tagname_By_Line()}]%)%=%(%c%V\ %p%%%)\ %f
"set statusline=%t[%{strlen(&fenc)?&fenc:'none'},%{&ff}]%h%m%r%y%=%c,%l/%L\ %P
set statusline=%t       "tail of the filename
set statusline+=[%{strlen(&fenc)?&fenc:'none'}, "file encoding
"set statusline+=[%{strlen(&ft)?&ft:'none'},  	" filetype
"set statusline+=%{synIDattr(synID(line('.'),col('.'),1),'name')}\
set statusline+=%b,0x%-8B\                   " current char
set statusline+=%-14.(%l,%c%V%)\ %<%P        " offset
set statusline+=%{&ff}] "file format
set statusline+=%h      "help file flag
set statusline+=%m%w    "modified flag
set statusline+=%r      "read only flag
set statusline+=%y      "filetype
set statusline+=%=      "left/right separator
set statusline+=%c,     "cursor column
set statusline+=%l/%L   "cursor line/total lines
set statusline+=\ %P    "percent through file
set statusline+=%F      " directory path
" for syntastic usage
"set statusline+=%#warningmsg#
"set statusline+=%{SyntasticStatuslineFlag()}
"set statusline+=%*

" indent settings
" turn this to on to make indent work
filetype indent on

" by default, turn it off
" and leave turning indent on in each language's config in FT_*()
autocmd BufRead,BufNewFile * filetype indent off

" omni completion
"set ofu=syntaxcomplete#Complete

" plugin neocomplcache
" let g:neocomplcache_enable_at_startup = 1

" find tags up to topdir
set tags=tags;/

" AutoTag plugin
" ==== Start Autotags Settings =======
let g:autotags_no_global = 0
let g:autotags_ctags_opts = "--exclude=target --exclude=vendor"
let g:autotags_ctags_languages = "+Scala,+Java,+Vim,+C,+CH,+CC,+CPP"
let g:autotags_ctags_langmap = "Scala:.scala,Java:.java,Vim:.vim,JavaScript:.js,C:.c,CH:.h,CC:.cc,CPP:cpp"
let g:autotags_ctags_global_include = ""
" " ==== End Start Autotags Settings ===

" taglist plugin
" set tag list
" display the current tag name in the window title bar
set title titlestring=%<%f\ %([%{Tlist_Get_Tagname_By_Line()}]%)
" display the current tag name in the status line
set statusline+=%<%f%=%([%{Tlist_Get_Tagname_By_Line()}]%)
:highlight MyTagListTagName guifg=blue ctermfg=blue
"let Tlist_Use_Right_Window = 1
let Tlist_Use_LEFT_Window=1
"let Tlist_Use_Horiz_Window = 1
let Tlist_Auto_Open = 1
let Tlist_WinHeight = 20
let Tlist_WinWidth = 40
let Tlist_File_Fold_Auto_Close=1
let Tlist_Show_One_File=1
let Tlist_Display_Prototype = 1
let Tlist_GainFocus_On_ToggleOpen=0
let Tlist_Display_Tag_Scope = 0
let Tlist_Exit_OnlyWindow=1
let Tlist_Compact_Format = 1
let Tlist_Max_Tag_Length = 10
let Tlist_Enable_Fold_Column = 1
let Tlist_Use_SingleClick = 1
let g:winManagerWindowLayout='FileExplorer'

" Nerdtree plugin
"autocmd vimenter * NERDTree " open Nerdtree automatically.
autocmd VimEnter * wincmd p
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
"autocmd vimenter * if !argc() | NERDTree | endif
let NERDTreeDirArrows=0
"let NERDTreeQuitOnOpen=1
"let NERDTreeMapOpenRecursively=1
"let NERDTreeMinimalUI=0
"let NERDTreeCasadeOpenSingleChildDir=0
"let NERDTreeAutoDeleteBuffer=1
let g:NERDTreeWinSize=15
let g:NERDTreeMapOpenSplit=1
let NERDTreeWinPos="right"
"let NERDTreeMapOpenVSplit=1
"let g:NERDTreeMapCloseDir=1
"let g:NERDTreeMapQuit=1
"let g:NERDTreeMapCloseChildren=1
"let g:NERDTreeQuitOnOpen=1
"let g:NERDTreeToggle=0

" plugin ctrl.vim
let g:ctrlp_use_caching = 1
let g:ctrlp_clear_cache_on_exit = 1
let g:ctrlp_by_filename = 1
let g:ctrlp_match_window = 'bottom,order:btt,min:1,max:10,results:10'
let g:ctrlp_tabpage_position = 'ac'
let g:ctrlp_cache_dir = $HOME.'/.cache/ctrlp'
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*        " Linux/MacOSX
let g:ctrlp_custom_ignore = '\v[\/]\.(git|hg|svn)$'

" plugin syntastic
"let g:syntastic_check_on_open = 1
"let g:syntastic_check_on_wq = 0 " don't check on :wq and :x
"let g:syntastic_enable_signs = 1 " errors on left side
"let g:syntastic_auto_loc_list = 2 " only show window when I ask
"let g:syntastic_cpp_check_header = 1
"let g:syntastic_cpp_no_include_search = 1
"let g:syntastic_c_compiler = 'gcc'
"let g:syntastic_c_compiler_options = ''
"let g:syntastic_javascript_checkers = ['jshint']
"let g:syntastic_javascript_jshint_conf = '$HOME/.jshintrc'
"let g:syntastic_html_jshint_conf = '$HOME/.jshintrc'
"let g:syntastic_cpp_compiler = 'g++'
"let g:syntastic_cpp_compiler_options = ' -std=c++11'
"if has('unix')
"	let g:syntastic_error_symbol = '★'
"	let g:syntastic_style_error_symbol = '>'
"	let g:syntastic_warning_symbol = '⚠'
"	let g:syntastic_style_warning_symbol = '>'
"else
"	let g:syntastic_error_symbol = '!'
"	let g:syntastic_style_error_symbol = '>'
"	let g:syntastic_warning_symbol = '.'
"	let g:syntastic_style_warning_symbol = '>'
"endif

" language specific settings
" Each language has its own style
" ======== language specific settings ===========

autocmd BufEnter *.cpp setf cpp
autocmd BufEnter *.c++ setf cpp
autocmd BufEnter *.cc setf cpp
autocmd BufEnter *.hpp setf cpp
autocmd BufEnter *.h setf c
autocmd BufEnter *.c setf c
autocmd BufEnter *.tex setf tex
autocmd BufEnter *.txt setf txt
autocmd BufEnter *.bib setf bib
autocmd BufEnter *.php setf php
autocmd BufEnter *.ml setf ocaml
autocmd BufEnter *.mli setf ocaml
autocmd BufEnter *.scala setf scala
autocmd BufEnter *.bash setf sh
autocmd BufEnter *.sh setf sh

autocmd FileType mail call FT_mail()
autocmd FileType cpp call FT_cpp()
autocmd FileType c call FT_c()
autocmd FileType php call FT_php()
autocmd FileType tex call FT_tex()
autocmd FileType txt call FT_txt()
autocmd FileType bib call FT_bib()
autocmd FileType ocaml call FT_ocaml()
autocmd FileType scala call FT_scala()
autocmd FileType sh call FT_sh()

function FT_sh()
    filetype indent on
endfunction

function FT_mail()
    set textwidth=100000000000
    " reformat for 72 char lines
    " normal gggqGgg
    " settings
    set spell spelllang=en
    set fileencodings=iso8859-1,utf-8
    " abbreviations
    " iabbr <buffer> gd Good Day!
endfunction

function FT_tex()
    " set textwidth=72
    " reformat for 72 char lines
    " normal gggqGgg
    set spell spelllang=en
    set fileencodings=iso8859-1,utf-8
    " setlocal fileencoding=iso8859-1,utf-8

    " ============= vim-latex ==================
    " REQUIRED. This makes vim invoke Latex-Suite when you open a tex file.
    filetype plugin on

    " IMPORTANT: win32 users will need to have 'shellslash' set so that latex
    " can be called correctly.
    set shellslash

    " IMPORTANT: grep will sometimes skip displaying the file name if you
    " search in a singe file. This will confuse Latex-Suite. Set your grep
    " program to always generate a file-name.
    set grepprg=grep\ -nH\ $*

    " OPTIONAL: This enables automatic indentation as you type.
    filetype indent on

    " OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults
    " to
    " 'plaintex' instead of 'tex', which results in vim-latex not being loaded.
    " The following changes the default filetype back to 'tex':
    let g:tex_flavor='latex'
    " ============= end vim-latex ==============
endfunction

function FT_txt()
    " set textwidth=68
    " reformat for 80 char lines
    " normal gggqGgg
    " set spell spelllang=en
    set nospell
    " setlocal fileencoding=iso8859-1,utf-8
    set fileencodings=iso8859-1,utf-8
endfunction

function FT_bib()
    set nospell
    set fileencodings=iso8859-1,utf-8
endfunction

function FT_php()
    set textwidth=100000000000
    " set textwidth=72
    set noautoindent
    set nocindent
    set nospell
endfunction

" ------------- C ----------------------------
function FT_c()
    " disable auto comment for C 
    setlocal comments-=:// comments+=f://
    set shiftwidth=4
    set tabstop=4
    " the textwidth is used for formatting the comments
    set textwidth=72
    set colorcolumn=72
    set autoindent
    set cindent
    set nospell
endfunction
" ------------- End C ----------------------------

" ------------- C++ ----------------------------
function FT_cpp()
    " disable auto comment for cpp
    setlocal comments-=:// comments+=f://
    set shiftwidth=2
    set tabstop=2
    set textwidth=72
    set colorcolumn=72
    set autoindent
    set cindent
    set nospell
endfunction

" syntastic
"let g:syntastic_cpp_compiler_options = ' -std=c++11'
"
" ------------- End C++ ----------------------------

" ------------------ OCaml --------------------

" ocp-indent with ocp-indent-vim
let s:opamshare=system("opam config var share | tr -d '\n'")
execute "autocmd FileType ocaml source ".s:opamshare."/vim/syntax/ocp-indent.vim"

let s:opamshare=system("opam config var share | tr -d '\n'")
" merlin
execute "set rtp+=".s:opamshare."/ocamlmerlin/vim"
execute "set rtp+=".s:opamshare."/ocamlmerlin/vimbufsync"

" merlin with syntastic
"let g:syntastic_ocaml_checkers=['merlin']

" merlin with neocomplcache
" if !exists('g:neocomplcache_force_omni_patterns')
"   let g:neocomplcache_force_omni_patterns = {}
" endif
" let g:neocomplcache_force_omni_patterns.ocaml = '[^. *\t]\.\w*\|\h\w*|#'

" syntastic
"let g:syntastic_ocaml_use_ocamlc = 1
"let g:syntastic_ocaml_use_janestreet_core = 1
"let s:opamlib=system("opam config var lib | tr -d '\n'")
"execute "let g:syntastic_ocaml_janestreet_core_dir = \"".s:opamlib."/core/\""
"let g:syntastic_ocaml_camlp4r = 1
"let g:syntastic_ocaml_use_ocamlbuild = 1

function FT_ocaml()
    " set textwidth=80
    set colorcolumn=80
    set shiftwidth=2
    set tabstop=2
    set autoindent
    " must before plugin indent on
    filetype indent on
    filetype plugin indent on

    " merlin with core
    " execute ":Use core"
endfunction

" ------------------ End OCaml --------------------

" ------------------ Scala --------------------

" disable syntastic for scala---it is too slow
" use sbt instead
let g:syntastic_scala_checkers=['']

function FT_scala()
    set colorcolumn=80
    set shiftwidth=2
    set tabstop=2
    set autoindent
    filetype indent on
    filetype plugin indent on
    " neocomplcache for scala
    " let g:neocomplcache_force_omni_patterns.scala = '[^. *\t]\.\w*\|\h\w*|#'
endfunction

" ------------------ End Scala --------------------
